$(function () {

    //点击已完成订单轨迹
    $("#show-finished-order-list").on('click','.finish-order-trace',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        setAllOrderTrace(node);
        $("#order-trace-info").modal('show');
    });
    //点击已完成删除按钮
    $("#show-finished-order-list").on('click','.finish-order-delete',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        //执行删除操作
        deleteAllFinishedOrder(node);
    });
    //点击已完成订单详情
    $("#show-finished-order-list").on('click','.finish-order-detail',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        allFinishedOrderInfo(node);
        $("#finished-order-info").modal('show');
    });
    //点击进行中订单详情
    $("#show-doing-order-list").on('click','.doing-order-detail',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        allDoingOrderInfo(node);
        $("#doing-order-info").modal('show');
    });
    //点击新下单订单详情
    $("#show-new-order-list").on('click','.new-order-detail',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        allNewOrderInfo(node);
        $("#new-order-info").modal('show');
    });


    //获取已完成订单列表
    $("#search-finished-order").click(function () {
        var date=$("#order-date-all").val();
        getAllFinishedOrder(date);
        //展示订单列表
        hideAllOrderTable();
        $("#show-finished-order-list").show();

    });
    //获取进行中订单列表
    $("#search-order-doing").click(function () {
        getAllDoingOrder();
        //展示订单列表
        hideAllOrderTable();
        $("#show-doing-order-list").show();
    });
    //获取新下单订单列表
    $("#search-order-new").click(function () {
        getAllNewOrder();
        //展示订单列表
        hideAllOrderTable();
        $("#show-new-order-list").show();
    });

});

function hideAllOrderTable() {
    $("#show-all-order-info .panel").hide();
}
//设置已完成订单轨迹
function setAllOrderTrace(node) {
    var tdArr=node.children();
    $("#table-order-create-time").val(node.attr("create-time"));
    $("#table-order-get-time").val(node.attr("get-time"));
    $("#table-order-finished-time").val(tdArr.eq(5).text());
}
//设置新下单详情
function allNewOrderInfo(node) {
    var tdArr=node.children();
    $("#table-new-order-id").val(tdArr.eq(0).text());//订单ID
    $("#table-new-order-customer-name").val(node.attr("customer-name"));//乘客姓名
    $("#table-new-order-customer-phone").val(node.attr("phone"));//乘客手机号
    $("#table-new-order-startPoint").val(tdArr.eq(1).text());//起点
    $("#table-new-order-destination").val(tdArr.eq(2).text());//目的地
    $("#table-new-order-distance").val(tdArr.eq(3).text()+"km");//距离
    $("#table-new-order-price").val(tdArr.eq(4).text()+"元");//价格
    $("#table-new-order-time").val(tdArr.eq(5).text());//下单时间
}
//设置进行中订单详情
function allDoingOrderInfo(node) {
    var tdArr=node.children();
    $("#table-get-order-id").val(tdArr.eq(0).text());//订单ID
    $("#table-get-order-customer-name").val(node.attr("customer-name"));//乘客姓名
    $("#table-get-order-customer-phone").val(node.attr("phone"));//乘客手机号
    $("#table-get-order-driver-name").val(node.attr("driver-name"));//司机姓名
    $("#table-get-order-startPoint").val(tdArr.eq(1).text());//起点
    $("#table-get-order-destination").val(tdArr.eq(2).text());//目的地
    $("#table-get-order-distance").val(tdArr.eq(3).text()+"km");//距离
    $("#table-get-order-price").val(tdArr.eq(4).text()+"元");//价格
    $("#table-get-order-time").val(tdArr.eq(5).text());//接单时间
}
//设置已完成订单详情
function allFinishedOrderInfo(node) {
    var tdArr=node.children();
    $("#table-finished-order-id").val(tdArr.eq(0).text());//订单ID
    $("#table-finished-order-customer-name").val(node.attr("customer-name"));//乘客姓名
    $("#table-finished-order-customer-phone").val(node.attr("phone"));//乘客手机号
    $("#table-finished-order-driver-name").val(node.attr("driver-name"));//司机姓名
    $("#table-finished-order-startPoint").val(tdArr.eq(1).text());//起点
    $("#table-finished-order-destination").val(tdArr.eq(2).text());//目的地
    $("#table-finished-order-distance").val(tdArr.eq(3).text()+"km");//距离
    $("#table-finished-order-price").val(tdArr.eq(4).text()+"元");//价格
    $("#table-finished-order-time").val(tdArr.eq(5).text());//完成时间
}

//获取已完成订单列表
function getAllFinishedOrder(selectedDate) {
    //通过账户获取乘客信息，然后以乘客Id获取订单信息
    if(selectedDate==null){
        showWarnMessage("日期为空!");
        return;
    }
    else{
        var dataInfo={
            account: "123456",
            selectedDay: new Date(selectedDate).getTime()
        };
        $.ajax({
            type: "POST",
            url: "/manager/get/all/finish/order/",
            contentType: "application/json",
            data: JSON.stringify(dataInfo),
            success: function (result) {
                if(result.status==0){
                    //执行写入订单列表
                    var orders=result.orders;
                    var customers=result.customers;
                    var parent=$("#finished-order-info-table");
                    clearAllFinishedOrder();
                    createAllFinishedOrder(orders,customers,parent);
                }else {
                    showWarnMessage("查询为空，当日没有订单记录！");
                }
            }
        });
    }
}
//获取乘客进行中订单列表
function getAllDoingOrder() {
    //通过账户获取乘客信息，然后以乘客Id获取订单信息
    $.ajax({
        type: "GET",
        url: "/manager/get/all/doing/order/",
        success: function (result){
            if(result.status==0){
                //执行写入订单列表
                var orders=result.orders;
                var customers=result.customers;
                var parent=$("#doing-order-info-table");
                clearAllDoingOrder()
                createAllDoingOrder(orders,customers,parent);
            }else{
                showWarnMessage("查询为空，当前并无进行中订单！");
            }
        }
    });

}
//获取乘客新下单列表
function getAllNewOrder() {
    //通过账户获取乘客信息，然后以乘客Id获取订单信息
    $.ajax({
        type: "GET",
        url: "/manager/get/all/new/order/",
        success: function (result) {
            if(result.status==0){
                //执行写入订单列表
                var orders=result.orders;
                var customers=result.customers;
                var parent=$("#new-order-info-table");
                clearAllNewOrder();
                createAllNewOrder(orders,customers,parent);
            }else{
                showWarnMessage("查询为空，当前并无新订单！");
            }
        }

    });

}
//删除已完成订单
function deleteAllFinishedOrder(node) {
    var tdArr=node.children();
    var orderId=tdArr.eq(0).text();
    //通过订单ID直接删除订单
    if(orderId.length==0){
        showWarnMessage("删除失败，订单ID不能为空！");
        return;
    }else{
        $.ajax({
            type: "GET",
            url: "/manager/delete/finish/order/"+orderId,
            Node: node,
            success: function (status) {
                if(status==0){
                    showWarnMessage("删除成功！");
                    //移除当前node
                    this.Node.remove();
                }else{
                    showWarnMessage("删除失败，未知原因！");
                }
            }
        });
    }
}

//订单信息
//清空已完成订单表
function clearAllFinishedOrder() {
    $("#finished-order-info-table tr:not(:first)").empty();
}
//清空进行中订单表
function clearAllDoingOrder() {
    $("#doing-order-info-table tr:not(:first)").empty();
}
//清空新下单订单表
function clearAllNewOrder() {
    $("#new-order-info-table tr:not(:first)").empty();
}

//写入已完成订单
function createAllFinishedOrder(orders,customers,parent) {
    for(var i=0;i<orders.length;i++){
        var orderId=orders[i].orderId;
        var driverName=orders[i].driverName;
        var customerName=customers[i];
        var start=orders[i].startPoint;
        var destination=orders[i].destination;
        var distance=orders[i].distance;
        var price=orders[i].price;
        var finishTime=getFormattedDateTime(orders[i].finishTime);
        //
        var getTime=getFormattedDateTime(orders[i].getTime);
        var createTime=getFormattedDateTime(orders[i].createTime);
        var phone=orders[i].phone;
        //添加
        var node=$('<tr></tr>');
        node.attr("customer-name",customerName);
        node.attr("driver-name",driverName);
        node.attr("phone",phone);
        node.attr("create-time",createTime);
        node.attr("get-time",getTime);
        node.append('<td>'+orderId+'</td>');
        node.append('<td>'+start+'</td>');
        node.append('<td>'+destination+'</td>');
        node.append('<td>'+distance+'</td>');
        node.append('<td>'+price+'</td>');
        node.append('<td>'+finishTime+'</td>');
        node.append('<td>\n' +
            '                                <button type="button" class="btn btn-default finish-order-delete">删除</button>\n' +
            '                                <button type="button" class="btn btn-success finish-order-trace">订单轨迹</button>\n' +
            '                                <button type="button" class="btn btn-info finish-order-detail">订单详情</button>\n' +
            '                            </td>');
        node.appendTo(parent);
    }
}
//写入进行中订单
function createAllDoingOrder(orders,customers,parent) {
    for(var i=0;i<orders.length;i++){
        var orderId=orders[i].orderId;
        var driverName=orders[i].driverName;
        var customerName=customers[i];
        var start=orders[i].startPoint;
        var destination=orders[i].destination;
        var distance=orders[i].distance;
        var price=orders[i].price;
        var getTime=getFormattedDateTime(orders[i].getTime);
        //
        var phone=orders[i].phone;
        //添加
        var node=$('<tr></tr>');
        node.attr("customer-name",customerName);
        node.attr("driver-name",driverName);
        node.attr("phone",phone);
        node.append('<td>'+orderId+'</td>');
        node.append('<td>'+start+'</td>');
        node.append('<td>'+destination+'</td>');
        node.append('<td>'+distance+'</td>');
        node.append('<td>'+price+'</td>');
        node.append('<td>'+getTime+'</td>');
        node.append('<td>\n' +
            '                                <button type="button" class="btn btn-info doing-order-detail">订单详情</button>\n' +
            '                            </td>');
        node.appendTo(parent);
    }
}
//写入新下单订单
function createAllNewOrder(orders,customers,parent) {
    for(var i=0;i<orders.length;i++){
        var orderId=orders[i].orderId;
        var customerName=customers[i];
        var start=orders[i].startPoint;
        var destination=orders[i].destination;
        var distance=orders[i].distance;
        var price=orders[i].price;
        var createTime=getFormattedDateTime(orders[i].createTime);
        //
        var phone=orders[i].phone;
        //添加
        var node=$('<tr></tr>');
        node.attr("customer-name",customerName);
        node.attr("phone",phone);
        node.append('<td>'+orderId+'</td>');
        node.append('<td>'+start+'</td>');
        node.append('<td>'+destination+'</td>');
        node.append('<td>'+distance+'</td>');
        node.append('<td>'+price+'</td>');
        node.append('<td>'+createTime+'</td>');
        node.append('<td>\n' +
            '                                <button type="button" class="btn btn-info new-order-detail">订单详情</button>\n' +
            '                            </td>');
        node.appendTo(parent);
    }
}
